<?php

namespace App\Http\Controllers\admin;

use App\Http\Model\Category;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

require_once 'App\Org\cate\Cate.class.php';
class CategoryController extends CommonController
{

    //分类列表
    public function index()
    {
        $category = Category::getlist();
        return view('admin.category.index')->with('data',$category);
    }
    //显示添加表单
    public function add()
    {
        $category = Category::getlist();
        return view('admin.category.add')->with('data',$category);
    }
    //处理添加表单
    public function addHandle()
    {
        $input = Input::except('_token');
        $rules = [
            'cate_name' => 'required',
        ];
        $message = [
            'cate_name.required' => '分类名称不能为空!'
        ];
        $validator = Validator::make($input, $rules, $message);
        if ($validator->passes()) {
            $row = Category::create($input);
            if ($row) {
                return redirect('admin/category/list');
            }
            return back()->with('errors', '添加失败!');
        } else {
            return back()->withErrors($validator);
        }

    }

//显示修改表单
    public function edit($cate_id)
    {
        $category = Category::getlist();
        $row = Category::find($cate_id);
        return view('admin.category.edit')->with(['data'=>$category,'row'=>$row]);
    }
//处理修改表单

//还有问题  待修复
    public function editHandle()
    {
        $input = Input::except('_token');
        $row = Category::find($input['cate_id']);
        if($row->cate_pid == 0){$input['cate_pid']=0;}
        $re = $row->update($input);
        if($re){
            return redirect('admin/category/list')->with('errors', '修改成功!');
        }
             return back()->with('errors', '修改失败!');


    }

//异步处理排序
    public function changeOrder(){
        $input = Input::all();
        $cateInfo = Category::find($input['cate_id']);
        $cateInfo->cate_order = $input['cate_ord'];
        $re = $cateInfo->update();
        if($re){
            $data = array(
                'error' => '0',
                'msg' =>'更新成功!'
            );
        }else{
            $data = array(
                'error' => '1',
                'msg' =>'更新失败!'
            );
        }
        return $data;

    }
    public function delCate()
    {
        $input = Input::except('_token');
        $row = Category::where($input)->delete();
        $child_ids = Category::getChilds($input['cate_id']);
        //如果存在子级分类  将子级分类改为顶级
        if($child_ids){
           Category::whereIn('cate_id', $child_ids)->update(['cate_pid'=>0]);
        }
        if($row){
            $data = [
                'error'=>'0',
                'msg'=>'删除成功!'
            ];
        }else{
            $data = [
                'error'=>'1',
                'msg'=>'删除失败'
            ];
        }
        return $data;
    }
}
